package Str;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者:hfj
 * 功能:228.汇总区间
 * 日期: 2025/10/28 19:58
 */
public class SummaryRanges {

    public static void main(String[] args) {
        int[] nums = {0,1,2,4,5,7};
        System.out.println(summaryRanges(nums));
    }

    public static List<String> summaryRanges(int[] nums) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            int cur = nums[i];
            StringBuilder builder = new StringBuilder();
            builder.append(cur);
            while (i < nums.length - 1 && nums[i+1] == nums[i] + 1){//有序数组从0开始,直到不连续不增加指针
                i++;
            }
            if (nums[i] != cur){
                builder.append("->");
                builder.append(nums[i]);
            }
            list.add(builder.toString());
        }
        return list;
    }
}
